<?php
namespace app\home\controller;

use app\common\model\WillNotice as WillNoticeModel;
use think\Db;
/**
 * 发挥预告控制器
 * Class Willnotice
 * @package app\index\controller
 */
class Willnotice extends Base
{
    /**
     * [index  查看]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $this->getPageAndSize($param);
        $model = new WillNoticeModel();
        $map = [];
        try {
            $map['status'] = 1;
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $time = date('Y-m-d H:i:s');
            $field = 'id,title,`from`,cover,create_time,';
            /*$sql = "select ".$field."
                (case when endtime >= '".$time."' and starttime <= '".$time."' then 1 
                    when starttime > '".$time."' then 2
                    when endtime < '".$time."' then 3 end) as states 
                from app_will_notice where status = 1 order by states asc limit ".$this->from.','.$this->size;*/
            $sql = "select ".$field."
                (case when endtime >= '".$time."' then 1
                    when endtime < '".$time."' then 2 end) as states 
                from app_will_notice where status = 1 order by states asc,id desc limit ".$this->from.','.$this->size;
            $lists = Db::query($sql);
            foreach($lists as &$v){
                $v['addtime'] = MessageTime($v['create_time']);
                if($v['states'] == 1){
                    $v['states'] = '进行中';
                }elseif($v['states'] == 2){
                    $v['states'] = '已结束';
                }
            }
        } catch (\Exception $e) {
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getcontent  获取内容]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getcontent()
    {
        $param = input('param.');
        $model = new WillNoticeModel();
        try{
            $data = $model->findData($param['id'],'id','id,title,cover,from,content,create_time,starttime');
        } catch (\Exception $e) {
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
}